package com.cnap.multiclouldmgmt.service.impl;

import cn.hutool.http.HttpRequest;
import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.ssh.SshConnection;
import com.cnap.multiclouldmgmt.model.ssh.SshResponse;
import com.cnap.multiclouldmgmt.model.vm.VmModel;
import com.cnap.multiclouldmgmt.service.CnapSshService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import com.cnap.multiclouldmgmt.util.SshUtils;
import com.cnap.multiclouldmgmt.util.TokenUtils;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class CnapSshServiceImplTest extends MulticlouldmgmtApplicationTests {


    @Autowired
    private CnapSshService cnapSshService;

    @Test
    void getPackageNormal() throws Exception {

        VmModel vmModel = new VmModel();

        SshResponse errRep = new SshResponse("","",1);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(errRep);
        cnapSshService.getPackageNormal(vmModel);
        mockSshUtils.close();

        SshResponse successRep = new SshResponse("7c9ed032d52ec3f86164dad3a7e75d72","",0);
        mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(successRep);
        cnapSshService.getPackageNormal(vmModel);
        mockSshUtils.close();


        SshResponse failRep = new SshResponse("7c","",0);
        mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(failRep);
        cnapSshService.getPackageNormal(vmModel);
        mockSshUtils.close();



    }

    @Test
    void copyMasterInstallPackage() throws Exception {

        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse errRep = new SshResponse("","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(errRep);
        cnapSshService.copyMasterInstallPackage(vmModel);
        mockSshUtils.close();

    }

    @Test
    void setNoPassWdCopy() throws Exception {

        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.setNoPassWdCopy("a","b","c");
        cnapSshService.setNoPassWdCopy("a","b","c","d","e");
        cnapSshService.addWorkerPrometheus(vmModel);
        mockSshUtils.close();


    }

    @Test
    void testSetNoPassWdCopy() throws Exception {
        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("","",1);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.setNoPassWdCopy("a","b","c");
        cnapSshService.setNoPassWdCopy("a","b","c","d","e");
        cnapSshService.addWorkerPrometheus(vmModel);
        mockSshUtils.close();
    }

    @Test
    void testSetNoPassWdCopy1() {
    }

    @Test
    void tarXvfInstallPackage() throws Exception {
        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.tarXvfInstallPackage(vmModel);
        mockSshUtils.close();
    }

    @Test
    void configInstallParamAndInstall() throws Exception {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.configInstallParamAndInstall(masterModel,vmModels);
        mockSshUtils.close();
    }

    @Test
    void getK8sToken() throws Exception {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("sace\\s*","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.getK8sToken(masterModel);
        mockSshUtils.close();
    }

    @Test
    void delHostMumo() throws Exception {
        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("sace\\s*","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.delHostMumo(masterModel,new VmModel());
        mockSshUtils.close();
    }

    @Test
    void installTools() throws Exception {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("sace\\s*","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.installTools(masterModel);
        mockSshUtils.close();
    }

    @Test
    void addK8sWorker() throws Exception {
        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("sace\\s*","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.addK8sWorker(masterModel,new VmModel());
        mockSshUtils.close();
    }

    @Test
    void setTencentRootSsh() throws Exception {
        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModels = new ArrayList<>();
        vmModels.add(new VmModel());

        SshResponse rep = new SshResponse("sace\\s*","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.setTencentRootSsh(masterModel);
        mockSshUtils.close();
    }

    @Test
    void preInstallCmds() throws Exception {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString(),
                Mockito.anyLong()))
                .thenReturn(rep);
        cnapSshService.preInstallCmds(masterModel);
        mockSshUtils.close();
    }

    @Test
    void testGetPackageNormal() throws Exception {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("7c9ed032d52ec3f86164dad3a7e75d72","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.getPackageNormal(masterModel);
        mockSshUtils.close();

    }


    @Test
    void testSetNoPassWdCopy2() throws Exception {
        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("7c9ed032d52e","",0);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString()))
                .thenReturn(rep);
        cnapSshService.getPackageNormal(masterModel);
        mockSshUtils.close();
    }



    @Test
    void delInnerIpNoPasswdSsh() {

        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("7c9ed032d52e","",1);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString(),
                Mockito.anyLong()))
                .thenReturn(rep);
        cnapSshService.delInnerIpNoPasswdSsh(masterModel,vmModel);
        mockSshUtils.close();
    }

    @Test
    void delExternalIpNoPasswdSsh() {
        VmModel masterModel = new VmModel();
        masterModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("7c9ed032d52e","",1);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString(),
                Mockito.anyLong()))
                .thenReturn(rep);
        cnapSshService.delExternalIpNoPasswdSsh(masterModel,vmModel);
        mockSshUtils.close();
    }

    @Test
    void checkVmNetConnectSuccess() {

        VmModel vmModel = new VmModel();
        vmModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);

        SshResponse rep = new SshResponse("7c9ed032d52e","",1);
        MockedStatic<SshUtils> mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString(),Mockito.anyLong()))
                .thenReturn(rep);
        cnapSshService.checkVmNetConnectSuccess(vmModel);
        mockSshUtils.close();

        rep = new SshResponse("7c9ed032d52e","",0);
        mockSshUtils = Mockito.mockStatic(SshUtils.class);
        mockSshUtils.when(() -> SshUtils.runCommand(Mockito.any(SshConnection.class),Mockito.anyString(),Mockito.anyLong()))
                .thenReturn(rep);
        cnapSshService.checkVmNetConnectSuccess(vmModel);
        mockSshUtils.close();
    }
}